package com.ruoyi.web.controller.pollution;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pollution.domain.BusPsCodOutputpollutant;
import com.ruoyi.pollution.domain.BusPsCodPollutant;
import com.ruoyi.pollution.service.IBusPsCodOutputpollutantService;
import com.ruoyi.pollution.service.IBusPsCodPollutantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pollution.domain.BusPsBasWateroutput;
import com.ruoyi.pollution.service.IBusPsBasWateroutputService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 废水排放口Controller
 * 
 * @author ruoyi
 * @date 2025-09-19
 */
@RestController
@RequestMapping("/pollution/wateroutput")
public class BusPsBasWateroutputController extends BaseController
{
    @Autowired
    private IBusPsBasWateroutputService busPsBasWateroutputService;
    @Autowired
    private IBusPsCodOutputpollutantService busPsCodOutputpollutantService;
    @Autowired
    private IBusPsCodPollutantService busPsCodPollutantService;

    /**
     * 查询废水排放口列表
     */
    //@PreAuthorize("@ss.hasPermi('pollution:wateroutput:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusPsBasWateroutput busPsBasWateroutput)
    {
        startPage();
        List<BusPsBasWateroutput> list = busPsBasWateroutputService.selectBusPsBasWateroutputList(busPsBasWateroutput);
        return getDataTable(list);
    }

    @GetMapping("/listall")
    public AjaxResult listall(BusPsBasWateroutput busPsBasWateroutput)
    {
        List<BusPsBasWateroutput> list = busPsBasWateroutputService.selectBusPsBasWateroutputList(busPsBasWateroutput);
        List<Map> mapList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for (BusPsBasWateroutput wateroutput : list) {
                Map map = new HashMap();
                map.put("outportcode",wateroutput.getOutportcode());
                map.put("outportname",wateroutput.getOutportname());
                mapList.add(map);
            }
        }
        return AjaxResult.success(mapList);
    }

    @PostMapping("/additems")
    public AjaxResult additems(@RequestBody BusPsBasWateroutput busPsBasWateroutput)
    {
        BusPsBasWateroutput oldBusPsBasWateroutput = busPsBasWateroutputService.selectBusPsBasWateroutputByOutportcode(busPsBasWateroutput.getOutportcode());
        BusPsCodOutputpollutant busPsCodOutputpollutantParam = new BusPsCodOutputpollutant();
        busPsCodOutputpollutantParam.setOutportcode(busPsBasWateroutput.getOutportcode());
        List<BusPsCodOutputpollutant> busPsCodOutputpollutantList = busPsCodOutputpollutantService.selectBusPsCodOutputpollutantList(busPsCodOutputpollutantParam);

        String itemcodes = busPsBasWateroutput.getPollutantcode();
        List<String> itemcodelist = new ArrayList<>();
        if(StringUtils.isNotEmpty(itemcodes)){
            String[] codeArray = itemcodes.split(",");
            for (String code : codeArray) {
                itemcodelist.add(code);
                String oldCode = "";
                if(!CollectionUtils.isEmpty(busPsCodOutputpollutantList)){
                    for (BusPsCodOutputpollutant codOutputpollutant : busPsCodOutputpollutantList) {
                        if(code.equals(codOutputpollutant.getPollutantcode())){
                            oldCode = codOutputpollutant.getPollutantcode();
                            break;
                        }
                    }
                }
                if(StringUtils.isEmpty(oldCode)){
                    BusPsCodOutputpollutant busPsCodOutputpollutant = new BusPsCodOutputpollutant();
                    busPsCodOutputpollutant.setPollutantcode(code);
                    busPsCodOutputpollutant.setOutportcode(oldBusPsBasWateroutput.getOutportcode());
                    busPsCodOutputpollutant.setEntcode(oldBusPsBasWateroutput.getEntcode());
                    busPsCodOutputpollutantService.insertBusPsCodOutputpollutant(busPsCodOutputpollutant);
                }
            }
        }
        for (BusPsCodOutputpollutant busPsCodOutputpollutant : busPsCodOutputpollutantList) {
            if(!itemcodelist.contains(busPsCodOutputpollutant.getPollutantcode())){
                busPsCodOutputpollutantService.deleteByParam(busPsCodOutputpollutant.getPollutantcode(),busPsCodOutputpollutant.getOutportcode(),busPsCodOutputpollutant.getEntcode());
            }
        }
        return AjaxResult.success();
    }

    /**
     * 导出废水排放口列表
     */
    //@PreAuthorize("@ss.hasPermi('pollution:wateroutput:export')")
    @Log(title = "废水排放口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusPsBasWateroutput busPsBasWateroutput)
    {
        List<BusPsBasWateroutput> list = busPsBasWateroutputService.selectBusPsBasWateroutputList(busPsBasWateroutput);
        ExcelUtil<BusPsBasWateroutput> util = new ExcelUtil<BusPsBasWateroutput>(BusPsBasWateroutput.class);
        util.exportExcel(response, list, "废水排放口数据");
    }

    /**
     * 获取废水排放口详细信息
     */
    //@PreAuthorize("@ss.hasPermi('pollution:wateroutput:query')")
    @GetMapping(value = "/{outportcode}")
    public AjaxResult getInfo(@PathVariable("outportcode") String outportcode)
    {
        BusPsBasWateroutput busPsBasWateroutput = busPsBasWateroutputService.selectBusPsBasWateroutputByOutportcode(outportcode);
        BusPsCodOutputpollutant busPsCodOutputpollutantParam = new BusPsCodOutputpollutant();
        busPsCodOutputpollutantParam.setOutportcode(busPsBasWateroutput.getOutportcode());
        List<BusPsCodOutputpollutant> busPsCodOutputpollutantList = busPsCodOutputpollutantService.selectBusPsCodOutputpollutantList(busPsCodOutputpollutantParam);
        if(!CollectionUtils.isEmpty(busPsCodOutputpollutantList)){
            busPsBasWateroutput.setPollutantList(busPsCodOutputpollutantList);
        }
        return success(busPsBasWateroutput);
    }

    /**
     * 新增废水排放口
     */
    //@PreAuthorize("@ss.hasPermi('pollution:wateroutput:add')")
    //@Log(title = "废水排放口", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BusPsBasWateroutput busPsBasWateroutput) throws IOException {
        return toAjax(busPsBasWateroutputService.insertBusPsBasWateroutput(busPsBasWateroutput));
    }

    /**
     * 修改废水排放口
     */
    //@PreAuthorize("@ss.hasPermi('pollution:wateroutput:edit')")
    //@Log(title = "废水排放口", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BusPsBasWateroutput busPsBasWateroutput) throws IOException {
        return toAjax(busPsBasWateroutputService.updateBusPsBasWateroutput(busPsBasWateroutput));
    }

    /**
     * 删除废水排放口
     */
    //@PreAuthorize("@ss.hasPermi('pollution:wateroutput:remove')")
    @Log(title = "废水排放口", businessType = BusinessType.DELETE)
	@PostMapping("/remove/{outportcodes}")
    public AjaxResult remove(@PathVariable String[] outportcodes)
    {
        return toAjax(busPsBasWateroutputService.deleteBusPsBasWateroutputByOutportcodes(outportcodes));
    }
}
